<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 2018/10/27
 * Time: 17:35
 */
namespace app\admin\controller;

use think\Config;
use app\common\controller\AdminBase;
use app\common\model\Csuser as CsuserModel;
use app\common\model\Kefu as KefuModel;
// 公众号表
use app\common\model\AuthorWxUser as AuthorWxUserModel;
// 通用标签 au_id = 0
use app\common\model\UserGroup as UGroupModel;
class Tags extends AdminBase{
    protected $_tags_ugroup_model;
    protected $_author_wx_user_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_tags_ugroup_model = new UGroupModel();
        $this->_author_wx_user_model = new AuthorWxUserModel();
        $tag_levels = $this->_tags_ugroup_model->getLevelList();
        $field = 'id,wxname';
        $weixins = $this->_author_wx_user_model->field($field)->select();
        $this->assign('tags_level_list',$tag_levels);
        $this->assign('weixins',$weixins);
    }
    /*
     * 显示所有标签
     * */
    public function index(){
        return $this->fetch();
    }

    /*
     * 新增标签
     * */
    public function add($pid = 0,$auid=0){
        return $this->fetch('add', ['pid' => $pid,'auid'=>$auid ]);
    }

    /*
     * 保存标签
     * */
    public function save(){
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            if($this->_tags_ugroup_model->allowField(true)->save($data)){
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /*
     * 编辑标签
     * */
    public function edit($id){
        $tags = $this->_tags_ugroup_model->find($id);
        return $this->fetch('edit',['tags'=>$tags]);
    }

    /*
     * 更新标签
     * */
    public function update($id){
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            if ($this->category_model->allowField(true)->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /*
     * 删除标签
     * */
    public function delete($id){
        // 判断是否pid=0
        $tag_pid = $this->_tags_ugroup_model->where('id',$id)->value('pid');
//        var_dump($tag_pid);
        $data['is_delete'] =  2;
        if($tag_pid){
            $deletetag = $this->_tags_ugroup_model->where('id',$id)->update($data);
            if ($deletetag) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }else{
            $has_sub = $this->_tags_ugroup_model->where('pid',$tag_pid)->select();
            if ($has_sub) {
                $this->success('存在子标签,不可删除');
            } else {
                $this->error('删除失败');
            }
        }
    }


    /*
     * 公用的标签
     * 显示所有的标签
     * */
    public function common_tags($page = 1){
        $field = 'id,auid, group_id, name, sort, pid, remark, color';
        // 显示公用的标签
        // $article_list  = $this->article_model->field($field)->where($map)->order(['publish_time' => 'DESC'])->paginate(15, false, ['page' => $page]);
        $tags_list = $this->_tags_ugroup_model->field($field)->order(['sort' => 'DESC'])->paginate(15, false, ['page' => $page]);
//        var_dump($tags_list);
        return json_encode(['status' => 1, 'msg' => $tags_list]);
    }

    /*
     *
     * */
    public function index7(){
//        echo 'hello';
        $field = 'id,auid, group_id, name, sort, pid, remark, color';
        // 标签二级分类
        $alltags = $this->_tags_ugroup_model->field($field)->order(['sort' => 'DESC'])->select();
        // 取出数组, 根据pid为0来分组

      $tags_arrays =   self::arrayToLevel($alltags);
//        var_dump($tags_arrays);
        return json_encode(['status' => 1, 'msg' => $tags_arrays]);

    }

    public function deal_tags(){
//        $field = 'id,auid, group_id, name, sort, pid, remark, color';
//        $alltags = $this->_tags_ugroup_model->field($field)->order(['sort' => 'DESC'])->select();
        $tag_levels = $this->_tags_ugroup_model->getLevelList();
        var_dump($tag_levels);
    }

    public function arrayToLevel($array, $pid = 0, $level = 1)
    {
        static $list = [];
        foreach ($array as $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = $level;
                $list[]     = $v;
                array2level($array, $v['id'], $level + 1);
            }
        }
        return $list;
    }

}